/**
 * Created on 2007-05-23 13:04
 */
package internet;

import game.Settings;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author David Wallin
 */
public class InternetReader
{
	private BufferedReader m_Reader;
	private DataInputStream m_Stream;
	private String m_DocumentLocation;
	private static final String TOP10_LOCATION = "http://david.pansarvagn.net/3tris/highscore/io.php?action=top10";
	private static final String HIGHSCORE_LOCATION = "http://david.pansarvagn.net/3tris/highscore/io.php?action=highscore";
	private static final String HIGHSCORE_SUBMIT = "http://david.pansarvagn.net/3tris/highscore/io.php?action=add";
	
	public static String getTop10Location()
	{
		return TOP10_LOCATION + "&difficulty=" + Settings.INSTANCE.getDifficulty().toString();
	}
	
	public static String getHighscoreLocation()
	{
		return HIGHSCORE_LOCATION + "&difficulty=" + Settings.INSTANCE.getDifficulty().toString();
	}
	
	public static String getHighscoreSubmitLocation()
	{
		return HIGHSCORE_SUBMIT + "&difficulty=" + Settings.INSTANCE.getDifficulty().toString();
	}
	
	public InternetReader()
	{
		this("");
	}
	
	public InternetReader(String documentLocation)
	{
		m_Reader = null;
		m_Stream = null;
		m_DocumentLocation = documentLocation;
	}
	
	/**
	 * Attempts to load the document found in the currently set address
	 * 
	 * @throws Exception if something went wrong
	 */
	public void loadDocument() throws Exception
	{
		if(m_DocumentLocation.equals(""))
		{
			throw new Exception("No Address was specified");
		}
		
		URL					url = new URL(m_DocumentLocation);
		URLConnection		connection = url.openConnection();
		
		connection.setDoInput(true);
		connection.setUseCaches(false);
		
		m_Stream = new DataInputStream(connection.getInputStream());
		m_Reader = new BufferedReader(new InputStreamReader(m_Stream));
	}
	
	public String readLine() throws IllegalAccessException
	{
		if(m_Reader == null)
		{
			try
			{
				loadDocument();
			}
			catch(Exception swallowed)
			{
			}
			
			if(m_Reader == null)
			{
				throw new IllegalAccessException("The reader has not been instantiated");
			}
		}
		
		String line = "";
		
		try
		{
			line = m_Reader.readLine();
		}
		catch(IOException e)
		{
			closeStreams();
			throw new IllegalAccessException("The end of the document was probably reached");
		}
		
		return line;
	}
	
	public DataInputStream getStream()
	{
		DataInputStream stream = m_Stream;
		closeStreams();
		
		return stream;
	}
	
	public void setDocumentLocation(String documentLocation)
	{
		m_DocumentLocation = documentLocation;
	}
	
	public String getDocumentLocation()
	{
		return m_DocumentLocation;
	}
	
	private void closeStreams()
	{
		try
		{
			m_Reader.close();
			m_Stream.close();
		}
		catch(IOException swallowed)
		{
		}
		
		m_Reader = null;
		m_Stream = null;
	}
}
